3 * Holds tests for DatabaseMysqlBase class.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
21 * @author Antoine Musso
22 * @copyright © 2013 Antoine Musso
23 * @copyright © 2013 Wikimedia Foundation and contributors
26 use Wikimedia\Rdbms\MySQLMasterPos
;
27 use Wikimedia\Rdbms\DatabaseMysqli
;
28 use Wikimedia\Rdbms\Database
;
29 use Wikimedia\TestingAccessWrapper
;
31 class DatabaseMysqlBaseTest
extends PHPUnit\Framework\TestCase
{
33 use MediaWikiCoversValidator
;
36 * @dataProvider provideDiapers
37 * @covers Wikimedia\Rdbms\DatabaseMysqlBase::addIdentifierQuotes
39 public function testAddIdentifierQuotes( $expected, $in ) {
40 $db = $this->getMockBuilder( DatabaseMysqli
::class )
41 ->disableOriginalConstructor()
45 $quoted = $db->addIdentifierQuotes( $in );
46 $this->assertEquals( $expected, $quoted );
50 * Feeds testAddIdentifierQuotes
52 * Named per T22281 convention.
54 public static function provideDiapers() {
56 // Format: expected, input
59 // Yeah I really hate loosely typed PHP idiocies nowadays
62 // Dear codereviewer, guess what addIdentifierQuotes()
63 // will return with thoses:
67 // We never know what could happen
71 // Whatchout! Should probably use something more meaningful
72 [ "`'`", "'" ], # single quote
73 [ '`"`', '"' ], # double quote
74 [ '````', '`' ], # backtick
75 [ '`’`', '’' ], # apostrophe (look at your encyclopedia)
77 // sneaky NUL bytes are lurking everywhere
79 [ '`xyzzy`', "\0x\0y\0z\0z\0y\0" ],
83 self
::createUnicodeString( '`\u0001a\uFFFFb`' ),
84 self
::createUnicodeString( '\u0001a\uFFFFb' )
87 self
::createUnicodeString( '`\u0001\uFFFF`' ),
88 self
::createUnicodeString( '\u0001\u0000\uFFFF\u0000' )
91 [ '`メインページ`', 'メインページ' ],
92 [ '`Басты_бет`', 'Басты_бет' ],
95 [ '`Alix`', 'Alix' ], # while( ! $recovered ) { sleep(); }
96 [ '`Backtick: ```', 'Backtick: `' ],
97 [ '`This is a test`', 'This is a test' ],
101 private static function createUnicodeString( $str ) {
102 return json_decode( '"' . $str . '"' );
105 private function getMockForViews() {
106 $db = $this->getMockBuilder( DatabaseMysqli
::class )
107 ->disableOriginalConstructor()
108 ->setMethods( [ 'fetchRow', 'query' ] )
111 $db->method( 'query' )
112 ->with( $this->anything() )
113 ->willReturn( new FakeResultWrapper( [
114 (object)[ 'Tables_in_' => 'view1' ],
115 (object)[ 'Tables_in_' => 'view2' ],
116 (object)[ 'Tables_in_' => 'myview' ]
123 * @covers Wikimedia\Rdbms\DatabaseMysqlBase::listViews
125 public function testListviews() {
126 $db = $this->getMockForViews();
128 $this->assertEquals( [ 'view1', 'view2', 'myview' ],
132 $this->assertEquals( [ 'view1', 'view2' ],
133 $db->listViews( 'view' ) );
134 $this->assertEquals( [ 'myview' ],
135 $db->listViews( 'my' ) );
136 $this->assertEquals( [],
137 $db->listViews( 'UNUSED_PREFIX' ) );
138 $this->assertEquals( [ 'view1', 'view2', 'myview' ],
139 $db->listViews( '' ) );
142 public function testBinLogName() {
143 $pos = new MySQLMasterPos( "db1052.2424/4643", 1 );
145 $this->assertEquals( "db1052", $pos->binlog
);
146 $this->assertEquals( "db1052.2424", $pos->getLogFile() );
147 $this->assertEquals( [ 2424, 4643 ], $pos->pos
);
151 * @dataProvider provideComparePositions
152 * @covers Wikimedia\Rdbms\MySQLMasterPos
154 public function testHasReached(
155 MySQLMasterPos
$lowerPos, MySQLMasterPos
$higherPos, $match, $hetero
158 $this->assertTrue( $lowerPos->channelsMatch( $higherPos ) );
161 // Each position is has one channel higher than the other
162 $this->assertFalse( $higherPos->hasReached( $lowerPos ) );
164 $this->assertTrue( $higherPos->hasReached( $lowerPos ) );
166 $this->assertTrue( $lowerPos->hasReached( $lowerPos ) );
167 $this->assertTrue( $higherPos->hasReached( $higherPos ) );
168 $this->assertFalse( $lowerPos->hasReached( $higherPos ) );
169 } else { // channels don't match
170 $this->assertFalse( $lowerPos->channelsMatch( $higherPos ) );
172 $this->assertFalse( $higherPos->hasReached( $lowerPos ) );
173 $this->assertFalse( $lowerPos->hasReached( $higherPos ) );
177 public static function provideComparePositions() {
178 $now = microtime( true );
183 new MySQLMasterPos( 'db1034-bin.000976/843431247', $now ),
184 new MySQLMasterPos( 'db1034-bin.000976/843431248', $now ),
189 new MySQLMasterPos( 'db1034-bin.000976/999', $now ),
190 new MySQLMasterPos( 'db1034-bin.000976/1000', $now ),
195 new MySQLMasterPos( 'db1034-bin.000976/999', $now ),
196 new MySQLMasterPos( 'db1035-bin.000976/1000', $now ),
202 new MySQLMasterPos( '3E11FA47-71CA-11E1-9E33-C80AA9429562:23', $now ),
203 new MySQLMasterPos( '3E11FA47-71CA-11E1-9E33-C80AA9429562:24', $now ),
208 new MySQLMasterPos( '3E11FA47-71CA-11E1-9E33-C80AA9429562:99', $now ),
209 new MySQLMasterPos( '3E11FA47-71CA-11E1-9E33-C80AA9429562:100', $now ),
214 new MySQLMasterPos( '3E11FA47-71CA-11E1-9E33-C80AA9429562:99', $now ),
215 new MySQLMasterPos( '1E11FA47-71CA-11E1-9E33-C80AA9429562:100', $now ),
219 // MariaDB GTID style
221 new MySQLMasterPos( '255-11-23', $now ),
222 new MySQLMasterPos( '255-11-24', $now ),
227 new MySQLMasterPos( '255-11-99', $now ),
228 new MySQLMasterPos( '255-11-100', $now ),
233 new MySQLMasterPos( '255-11-999', $now ),
234 new MySQLMasterPos( '254-11-1000', $now ),
239 new MySQLMasterPos( '255-11-23,256-12-50', $now ),
240 new MySQLMasterPos( '255-11-24', $now ),
245 new MySQLMasterPos( '255-11-99,256-12-50,257-12-50', $now ),
246 new MySQLMasterPos( '255-11-1000', $now ),
251 new MySQLMasterPos( '255-11-23,256-12-50', $now ),
252 new MySQLMasterPos( '255-11-24,155-52-63', $now ),
257 new MySQLMasterPos( '255-11-99,256-12-50,257-12-50', $now ),
258 new MySQLMasterPos( '255-11-1000,256-12-51', $now ),
263 new MySQLMasterPos( '255-11-99,256-12-50', $now ),
264 new MySQLMasterPos( '255-13-1000,256-14-49', $now ),
269 new MySQLMasterPos( '253-11-999,255-11-999', $now ),
270 new MySQLMasterPos( '254-11-1000', $now ),
278 * @dataProvider provideChannelPositions
279 * @covers Wikimedia\Rdbms\MySQLMasterPos
281 public function testChannelsMatch( MySQLMasterPos
$pos1, MySQLMasterPos
$pos2, $matches ) {
282 $this->assertEquals( $matches, $pos1->channelsMatch( $pos2 ) );
283 $this->assertEquals( $matches, $pos2->channelsMatch( $pos1 ) );
285 $roundtripPos = new MySQLMasterPos( (string)$pos1, 1 );
286 $this->assertEquals( (string)$pos1, (string)$roundtripPos );
289 public static function provideChannelPositions() {
290 $now = microtime( true );
294 new MySQLMasterPos( 'db1034-bin.000876/44', $now ),
295 new MySQLMasterPos( 'db1034-bin.000976/74', $now ),
299 new MySQLMasterPos( 'db1052-bin.000976/999', $now ),
300 new MySQLMasterPos( 'db1052-bin.000976/1000', $now ),
304 new MySQLMasterPos( 'db1066-bin.000976/9999', $now ),
305 new MySQLMasterPos( 'db1035-bin.000976/10000', $now ),
309 new MySQLMasterPos( 'db1066-bin.000976/9999', $now ),
310 new MySQLMasterPos( 'trump2016.000976/10000', $now ),
317 * @dataProvider provideCommonDomainGTIDs
318 * @covers Wikimedia\Rdbms\MySQLMasterPos
320 public function testCommonGtidDomains( MySQLMasterPos
$pos, MySQLMasterPos
$ref, $gtids ) {
321 $this->assertEquals( $gtids, MySQLMasterPos
::getCommonDomainGTIDs( $pos, $ref ) );
324 public static function provideCommonDomainGTIDs() {
327 new MySQLMasterPos( '255-13-99,256-12-50,257-14-50', 1 ),
328 new MySQLMasterPos( '255-11-1000', 1 ),
333 '2E11FA47-71CA-11E1-9E33-C80AA9429562:5,' .
334 '3E11FA47-71CA-11E1-9E33-C80AA9429562:99,' .
335 '7E11FA47-71CA-11E1-9E33-C80AA9429562:30',
339 '1E11FA47-71CA-11E1-9E33-C80AA9429562:100,' .
340 '3E11FA47-71CA-11E1-9E33-C80AA9429562:66',
343 [ '3E11FA47-71CA-11E1-9E33-C80AA9429562:99' ]
349 * @dataProvider provideLagAmounts
350 * @covers Wikimedia\Rdbms\DatabaseMysqlBase::getLag
351 * @covers Wikimedia\Rdbms\DatabaseMysqlBase::getLagFromPtHeartbeat
353 public function testPtHeartbeat( $lag ) {
354 $db = $this->getMockBuilder( DatabaseMysqli
::class )
355 ->disableOriginalConstructor()
357 'getLagDetectionMethod', 'getHeartbeatData', 'getMasterServerInfo' ] )
360 $db->method( 'getLagDetectionMethod' )
361 ->willReturn( 'pt-heartbeat' );
363 $db->method( 'getMasterServerInfo' )
364 ->willReturn( [ 'serverId' => 172, 'asOf' => time() ] );
366 // Fake the current time.
367 list( $nowSecFrac, $nowSec ) = explode( ' ', microtime() );
368 $now = (float)$nowSec +
(float)$nowSecFrac;
369 // Fake the heartbeat time.
370 // Work arounds for weak DataTime microseconds support.
371 $ptTime = $now - $lag;
372 $ptSec = (int)$ptTime;
373 $ptSecFrac = ( $ptTime - $ptSec );
374 $ptDateTime = new DateTime( "@$ptSec" );
375 $ptTimeISO = $ptDateTime->format( 'Y-m-d\TH:i:s' );
376 $ptTimeISO .= ltrim( number_format( $ptSecFrac, 6 ), '0' );
378 $db->method( 'getHeartbeatData' )
379 ->with( [ 'server_id' => 172 ] )
380 ->willReturn( [ $ptTimeISO, $now ] );
382 $db->setLBInfo( 'clusterMasterHost', 'db1052' );
383 $lagEst = $db->getLag();
385 $this->assertGreaterThan( $lag - 0.010, $lagEst, "Correct heatbeat lag" );
386 $this->assertLessThan( $lag +
0.010, $lagEst, "Correct heatbeat lag" );
389 public static function provideLagAmounts() {
403 * @expectedException UnexpectedValueException
404 * @covers Wikimedia\Rdbms\Database::setFlag
406 public function testDBOIgnoreSet() {
407 $db = $this->getMockBuilder( DatabaseMysqli
::class )
408 ->disableOriginalConstructor()
412 $db->clearFlag( Database
::DBO_IGNORE
);
416 * @expectedException UnexpectedValueException
417 * @covers Wikimedia\Rdbms\Database::clearFlag
419 public function testDBOIgnoreClear() {
420 $db = $this->getMockBuilder( DatabaseMysqli
::class )
421 ->disableOriginalConstructor()
425 $db->clearFlag( Database
::DBO_IGNORE
);
429 * @covers Wikimedia\Rdbms\MySQLMasterPos
431 public function testSerialize() {
432 $pos = new MySQLMasterPos( '3E11FA47-71CA-11E1-9E33-C80AA9429562:99', 53636363 );
433 $roundtripPos = unserialize( serialize( $pos ) );
435 $this->assertEquals( $pos, $roundtripPos );
437 $pos = new MySQLMasterPos( '255-11-23', 53636363 );
438 $roundtripPos = unserialize( serialize( $pos ) );
440 $this->assertEquals( $pos, $roundtripPos );
444 * @covers Wikimedia\Rdbms\DatabaseMysqlBase::isInsertSelectSafe
445 * @dataProvider provideInsertSelectCases
447 public function testInsertSelectIsSafe( $insertOpts, $selectOpts, $row, $safe ) {
448 $db = $this->getMockBuilder( DatabaseMysqli
::class )
449 ->disableOriginalConstructor()
450 ->setMethods( [ 'getReplicationSafetyInfo' ] )
452 $db->method( 'getReplicationSafetyInfo' )->willReturn( (object)$row );
453 $dbw = TestingAccessWrapper
::newFromObject( $db );
455 $this->assertEquals( $safe, $dbw->isInsertSelectSafe( $insertOpts, $selectOpts ) );
458 public function provideInsertSelectCases() {
464 'innodb_autoinc_lock_mode' => '2',
465 'binlog_format' => 'ROW',
473 'innodb_autoinc_lock_mode' => '2',
474 'binlog_format' => 'ROW',
482 'innodb_autoinc_lock_mode' => '0',
483 'binlog_format' => 'STATEMENT',
491 'innodb_autoinc_lock_mode' => '2',
492 'binlog_format' => 'STATEMENT',
497 [ 'NO_AUTO_COLUMNS' ],
500 'innodb_autoinc_lock_mode' => '0',
501 'binlog_format' => 'STATEMENT',
509 'innodb_autoinc_lock_mode' => 0,
510 'binlog_format' => 'STATEMENT',
515 [ 'NO_AUTO_COLUMNS' ],
518 'innodb_autoinc_lock_mode' => 2,
519 'binlog_format' => 'STATEMENT',
524 [ 'NO_AUTO_COLUMNS' ],
527 'innodb_autoinc_lock_mode' => 0,
528 'binlog_format' => 'STATEMENT',
537 * @covers \Wikimedia\Rdbms\DatabaseMysqlBase::buildIntegerCast
539 public function testBuildIntegerCast() {
540 $db = $this->getMockBuilder( DatabaseMysqli
::class )
541 ->disableOriginalConstructor()
544 $output = $db->buildIntegerCast( 'fieldName' );
545 $this->assertSame( 'CAST( fieldName AS SIGNED )', $output );